package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.world.MapNode;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.utils.StringUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;

public class MapNodeEditor extends Shell {
	private Text name_text;
	private Text icon_text;
	private IconView canvas;
	private Spinner x_spinner;
	private Spinner y_spinner;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			MapNodeEditor shell = new MapNodeEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @wbp.parser.constructor
	 */
	public MapNodeEditor(Display display, final EventListener<MapNode> listener) {
		this(display, null, listener);
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public MapNodeEditor(Display display, MapNode mapNode, final EventListener<MapNode> listener) {
		super(display, SWT.DIALOG_TRIM);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(10, 13, 35, 17);
		lblNewLabel.setText("\u540D\u79F0 : ");

		name_text = new Text(this, SWT.BORDER);
		name_text.setBounds(51, 10, 383, 23);

		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(10, 240, 35, 17);
		lblNewLabel_1.setText("\u5750\u6807 : ");

		x_spinner = new Spinner(this, SWT.BORDER);
		x_spinner.setMaximum(65535);
		x_spinner.setBounds(51, 237, 80, 23);

		y_spinner = new Spinner(this, SWT.BORDER);
		y_spinner.setMaximum(65535);
		y_spinner.setBounds(137, 237, 80, 23);

		Label lblNewLabel_2 = new Label(this, SWT.NONE);
		lblNewLabel_2.setBounds(10, 42, 35, 17);
		lblNewLabel_2.setText("\u56FE\u7247 : ");

		icon_text = new Text(this, SWT.BORDER);
		icon_text.setBounds(51, 39, 297, 23);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					icon_text.setText(path);
				}
				canvas.setImage(path);
			}
		});
		btnNewButton.setBounds(354, 37, 80, 27);
		btnNewButton.setText("\u9009\u62E9\u6587\u4EF6");

		canvas = new IconView(this);
		canvas.setBounds(10, 65, 424, 166);

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					MapNode mapNode = new MapNode();
					mapNode.name = name_text.getText();
					mapNode.icon = icon_text.getText();
					mapNode.x = x_spinner.getSelection();
					mapNode.y = y_spinner.getSelection();
					listener.onEvent(mapNode);
				}
				dispose();
			}
		});
		btnNewButton_1.setBounds(354, 235, 80, 27);
		btnNewButton_1.setText("\u5B8C\u6210");
		createContents();

		if (mapNode != null) {
			name_text.setText(StringUtil.stringOf(mapNode.name));
			icon_text.setText(StringUtil.stringOf(mapNode.icon));
			x_spinner.setSelection(mapNode.x);
			y_spinner.setSelection(mapNode.y);
			canvas.setImage(mapNode.icon);
		}
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u5730\u56FE\u8282\u70B9\u8BBE\u7F6E");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
